﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Windows.Forms;
using DLL.Entities;


namespace DLL.Entities
{


    public class ReadXML
    {
        #region Declarations
        COM_Config CC_Obj = new COM_Config();
        RAD_Config RC_Obj = new RAD_Config();
        MIS_Config MC_Obj = new MIS_Config();

        Radars RS_Obj = new Radars();
        MISStatus MS_Obj = new MISStatus();

        Targets RE_Obj = new Targets();

        Missile[] MIS_Obj = new Missile[5];

        XmlReader reader = null;
        #endregion

        #region Read Configs
        public COM_Config ReadCOMConfig()
        {
            try
            {
                reader = new XmlTextReader("../../../DLL/XML/COM_Config.xml");
                XmlSerializer serializer = new XmlSerializer(typeof(COM_Config));
                CC_Obj = (COM_Config)serializer.Deserialize(reader);
            }

            catch (XmlException xe)
            {
                MessageBox.Show(xe.Message, "XML Parse Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (InvalidOperationException ioe)
            {
                MessageBox.Show(ioe.InnerException.Message, "XML Serialization Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception e1)
            {
                MessageBox.Show(Convert.ToString(e1));
            }
            finally
            {
                reader.Close();
            }

            return CC_Obj;
        }

        public RAD_Config ReadRADConfig()
        {
            try
            {
                reader = new XmlTextReader("../../../DLL/XML/RAD_Config.xml");
                XmlSerializer serializer = new XmlSerializer(typeof(RAD_Config));
                RC_Obj = (RAD_Config)serializer.Deserialize(reader);
            }

            catch (XmlException xe)
            {
                MessageBox.Show(xe.Message, "XML Parse Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (InvalidOperationException ioe)
            {
                MessageBox.Show(ioe.InnerException.Message, "XML Serialization Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception e1)
            {
                MessageBox.Show(Convert.ToString(e1));
            }
            finally
            {
                reader.Close();
            }

            return RC_Obj;
        }

        public MIS_Config ReadMISConfig()
        {
            try
            {
                reader = new XmlTextReader("../../../DLL/XML/MIS_Config.xml");//Creates a new serializer object
                XmlSerializer serializer = new XmlSerializer(typeof(MIS_Config));//Reads the xml file   
                MC_Obj = (MIS_Config)serializer.Deserialize(reader); //Sets the elements in the array
            }

            catch (XmlException xe)
            {
                MessageBox.Show(xe.Message, "XML Parse Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                //Displays an error if the XML could not be parsed
            }
            catch (InvalidOperationException ioe)
            {
                MessageBox.Show(ioe.InnerException.Message, "XML Serialization Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                //Displays an error if the XML could not be Deserialized
            }
            catch (Exception e1)
            {
                MessageBox.Show(Convert.ToString(e1));
                //Displays any other error
            }
            finally
            {
                reader.Close();
                //Closes the reader
            }

            return MC_Obj;
        }
        #endregion

        #region Read Status
        public Radars ReadRADStatus(string dir)
        {
            try
            {
                reader = new XmlTextReader(dir);
                XmlSerializer serializer = new XmlSerializer(typeof(Radars));
                RS_Obj = (Radars)serializer.Deserialize(reader);
            }

            catch (XmlException xe)
            {
                MessageBox.Show(xe.Message, "XML Parse Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (InvalidOperationException ioe)
            {
                MessageBox.Show(ioe.InnerException.Message, "XML Serialization Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception e1)
            {
                MessageBox.Show(Convert.ToString(e1));
            }
            finally
            {
                reader.Close();
            }

            return RS_Obj;
        }

        public MISStatus ReadMISStatus(string dir)
        {
            try
            {
                reader = new XmlTextReader(dir);
                XmlSerializer serializer = new XmlSerializer(typeof(MISStatus));
                MS_Obj = (MISStatus)serializer.Deserialize(reader);
            }

            catch (XmlException xe)
            {
                MessageBox.Show(xe.Message, "XML Parse Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (InvalidOperationException ioe)
            {
                MessageBox.Show(ioe.InnerException.Message, "XML Serialization Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception e1)
            {
                MessageBox.Show(Convert.ToString(e1));
            }
            finally
            {
                reader.Close();
            }

            return MS_Obj;
        }
        #endregion

        #region Read Events
        public Targets ReadRADEvent(string dir)
        {
            try
            {
                reader = new XmlTextReader(dir);
                XmlSerializer serializer = new XmlSerializer(typeof(Targets));
                RE_Obj = (Targets)serializer.Deserialize(reader);
            }

            catch (XmlException xe)
            {
                MessageBox.Show(xe.Message, "XML Parse Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (InvalidOperationException ioe)
            {
                MessageBox.Show(ioe.InnerException.Message, "XML Serialization Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception e1)
            {
                MessageBox.Show(Convert.ToString(e1));
            }
            finally
            {
                reader.Close();
            }

            return RE_Obj;
        }

        #endregion

        public Missile[] ReadMissile(string dir)
        {
            try
            {
                reader = new XmlTextReader(dir);
                XmlSerializer serializer = new XmlSerializer(typeof(Missile[]));
                MIS_Obj = (Missile[])serializer.Deserialize(reader);
            }

            catch (XmlException xe)
            {
                MessageBox.Show(xe.Message, "XML Parse Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (InvalidOperationException ioe)
            {
                MessageBox.Show(ioe.InnerException.Message, "XML Serialization Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception e1)
            {
                MessageBox.Show(Convert.ToString(e1));
            }
            finally
            {
                reader.Close();
            }

            return MIS_Obj;
        }
    }
}
